<?php
// +----------------------------------------------------------------------
// | HKCMS 应用调度信息
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2019 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace common\behavior;

class AppDispatchBehavior
{
    public function run(&$param)
    {
        if (!empty($_GET)) {
            $tempArr = $_GET;
            foreach ($_GET as $key=>$value) {
                $key = str_replace('amp;','',$key);
                $tempArr[$key] = $value;
            }
            $_GET = $tempArr;
        }

        if (!empty($_GET['m']) || !empty($_GET['c']) || !empty($_GET['a'])) {

            $route = [null, null, null];
            if (!empty($_GET['m'])) {
                if (!preg_match('/^[A-Za-z](\/|\w)*$/', $_GET['m'])) {
                    return false;
                }
                $route[0] = $_GET['m'];
            } else {
                if (defined('BIND_MODULE')) {
                    $route[0] = BIND_MODULE;
                } else {
                    $route[0] = config('default_module');
                }
            }
            if (!empty($_GET['c'])) {
                if (!preg_match('/^[A-Za-z](\/|\w)*$/', $_GET['c'])) {
                    return false;
                }
                $route[1] = $_GET['c'];
            } else {
                $route[1] = config('default_controller');
            }
            if (!empty($_GET['a'])) {
                if (!preg_match('/^[A-Za-z](\/|\w)*$/', $_GET['a'])) {
                    return false;
                }
                $route[2] = $_GET['a'];
            } else {
                $route[2] = config('default_action');
            }

            $param = ['type' => 'module', 'module' => $route];
        }
    }
}